#include "baclientdlg.h"
#include "ui_baclientdlg.h"

BAClientDlg::BAClientDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::BAClientDlg)
{
    ui->setupUi(this);
    ui->serverIP->setText("127.0.0.1");
    ui->serverPort->setText("2222");
    connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(showErrors()));
    connect(tcpSocket, SIGNAL(connected()), this, SLOT(on_connect_host()));
}

BAClientDlg::~BAClientDlg()
{
    delete ui;
}

void BAClientDlg::on_buttonBox_accepted()
{
    //建立IP连接
    ipAddr = ui->serverIP->text();
    port = ui->serverPort->text().toUShort();
    tcpSocket = new QTcpSocket(this);
    tcpSocket->connectToHost(ipAddr, port);
    on_connect_host();
}

void BAClientDlg::on_connect_host()
{
    MainBAClientDlg *mainwind = new MainBAClientDlg(tcpSocket, this);
    mainwind->show();
}

void BAClientDlg::showErrors()
{
    QMessageBox *errorMBox = new QMessageBox(this);
    errorMBox->warning(this, "Warning", tcpSocket->errorString());
}
